package com.gzdh.tang.dishrecognition.controller;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.tang.comm.util.GZip;
import com.gzdh.tang.comm.vo.Result;
import com.gzdh.tang.dishrecognition.dto.HosMenuDto;
import com.gzdh.tang.dishrecognition.dto.MenuLBDto;
import com.gzdh.tang.dishrecognition.dto.NWeekMenuBakDto;
import com.gzdh.tang.dishrecognition.mapper.DrPublicMapper;
import com.gzdh.tang.dishrecognition.util.ChineseCharacterHelper;
import com.gzdh.tang.mobile.entity.Bmcm;
import com.gzdh.tang.mobile.service.BmcmService;
import com.gzdh.tang.modu.entity.Bmzl;
import com.gzdh.tang.modu.entity.MacMainpSbruan;
import com.gzdh.tang.modu.service.BmzlService;
import com.gzdh.tang.modu.service.MacMainpSbruanService;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("dr/weekMenu")
@RestController
public class DrWeekMenuController {


    @Autowired
    MacMainpSbruanService macMainpSbruanService;

    @Autowired
    DrPublicMapper drPublicMapper;

    @Autowired
    BmzlService bmzlService;

    @Autowired
    BmcmService bmcmService;

    /**
     * 加载有什么菜谱
     * 有什么餐次
     * 有什么灶类
     * 整张HosMenu
     *
     * @return
     */

    @GetMapping("info")
    public Result<Object> getInfo(@RequestParam("deviceId") String deviceId, @RequestParam("table") int table) {
        Result<Object> result = new Result<>();
        MacMainpSbruan macMainpSbruan = macMainpSbruanService.getOne(new QueryWrapper<MacMainpSbruan>()
                .lambda().eq(MacMainpSbruan::getMac, deviceId));

        List<HosMenuDto> hosMenuDtoList = drPublicMapper.getHosMenu(macMainpSbruan.getAddr());
        for (HosMenuDto goo : hosMenuDtoList) {
            goo.setName(goo.getName().trim());
            goo.setBm(goo.getBm().trim());
            goo.setLbbm(goo.getLbbm().trim());
        }

        List<MenuLBDto> menuLBDtoList = drPublicMapper.getMenuLbDto();
        for (MenuLBDto goo : menuLBDtoList) {
            goo.setLbbm(goo.getLbbm().trim());
            goo.setLbname(goo.getLbname().trim());
        }

        List<Bmzl> bmzlList = bmzlService.list(new QueryWrapper<Bmzl>().lambda().eq(Bmzl::getAddr, macMainpSbruan.getAddr()));
        for (Bmzl goo : bmzlList) {
            goo.setBm(goo.getBm().trim());
            goo.setName(goo.getName().trim());
        }

        List<Bmcm> bmcmList = bmcmService.list();
        for (Bmcm goo : bmcmList) {
            goo.setBm(goo.getBm().trim());
            goo.setName(goo.getName());
        }
        List<String> savedmList = drPublicMapper.getSaveDm(table == 0 ? "zweekmenubak" : "weekmenubak");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hosMenuDtoList", hosMenuDtoList);
        jsonObject.put("menuLBDtoList", menuLBDtoList);
        jsonObject.put("bmzlList", bmzlList);
        jsonObject.put("bmcmList", bmcmList);
        jsonObject.put("savedmList", savedmList);
        result.setResult(GZip.gzip(jsonObject.toString()));

        return result;
    }


    @GetMapping("getWeekMenuBak")
    public Result<Object> getWeekMenuBak(@RequestParam("deviceId") String deviceId,
                                         @RequestParam("date") String date,
                                         @RequestParam("savedm") String savedm,
                                         @RequestParam("tableName") int tableName) {
        Result<Object> result = new Result<>();
        MacMainpSbruan macMainpSbruan = macMainpSbruanService.getOne(new QueryWrapper<MacMainpSbruan>()
                .lambda().eq(MacMainpSbruan::getMac, deviceId));
        List<NWeekMenuBakDto> nWeekMenuBakDtoList = drPublicMapper.getWeekMenu(tableName == 0 ? "zweekmenuBak" : "weekmenubak",
                savedm, date, macMainpSbruan.getAddr());
        for (NWeekMenuBakDto goo : nWeekMenuBakDtoList) {
            goo.setName(goo.getName().trim());
            goo.setMcjc(goo.getMcjc().trim());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("nWeekMenuBakDtoList", nWeekMenuBakDtoList);
        result.setResult(GZip.gzip(jsonObject.toString()));
        return result;
    }

    @PostMapping("saveWeekMenuBak")
    public Result<Object> saveWeekBak(@RequestBody JSONObject jsonObject) throws RuntimeException, ParseException {
        Result<Object> result = new Result<>();
        System.out.println(jsonObject.toString());
        String deviceId = jsonObject.getString("deviceId");
        MacMainpSbruan macMainpSbruan = macMainpSbruanService.getOne(new QueryWrapper<MacMainpSbruan>()
                .lambda().eq(MacMainpSbruan::getMac, deviceId));
        JSONArray jsonArray = jsonObject.getJSONArray("nWeekMenuBakDtoList");
        String savedm = jsonObject.getString("savedm");
        String date = jsonObject.getString("date");
        int tableName = jsonObject.getInteger("table");
        List<NWeekMenuBakDto> reqList = com.alibaba.fastjson.JSONArray.parseArray(jsonArray.toJSONString(), NWeekMenuBakDto.class);
        List<NWeekMenuBakDto> deleteWeekMenu = reqList.stream().filter(NWeekMenuBakDto::isDelete)
                .filter(nWeekMenuBakDto -> StringUtils.isNotEmpty(nWeekMenuBakDto.getPkid())).collect(Collectors.toList());
        for (NWeekMenuBakDto goo : deleteWeekMenu) {
            drPublicMapper.deleteNWeekMenuBakByPkid(tableName == 0 ? "zweekmenuBak" : "weekmenubak",
                    goo.getPkid());
        }

        List<NWeekMenuBakDto> addWeekMenu = reqList.stream().filter(nWeekMenuBakDto -> !nWeekMenuBakDto.isDelete())
                .filter(nWeekMenuBakDto -> StringUtils.isEmpty(nWeekMenuBakDto.getPkid())).collect(Collectors.toList());

        Date dates = new SimpleDateFormat("yyyy-MM-dd").parse(date);
        String dayNum = new SimpleDateFormat("E").format(dates);

        if (dayNum.contains("一")) {
            dayNum = "1";
        } else if (dayNum.contains("二")) {
            dayNum = "2";
        } else if (dayNum.contains("三")) {
            dayNum = "3";
        } else if (dayNum.contains("四")) {
            dayNum = "4";
        } else if (dayNum.contains("五")) {
            dayNum = "5";
        } else if (dayNum.contains("六")) {
            dayNum = "6";
        } else {
            dayNum = "7";
        }
        String year = new SimpleDateFormat("yyyy").format(dates);
        String month = new SimpleDateFormat("MM").format(dates);
        String day = new SimpleDateFormat("dd").format(dates);
        for (NWeekMenuBakDto goo : addWeekMenu) {
            goo.setDay(dayNum);
            goo.setAddr(macMainpSbruan.getAddr());
            drPublicMapper.insertIntoWeekMenuBak(tableName == 0 ? "zweekmenuBak" : "weekmenubak", goo, savedm, year, month, day
            );
        }
        return result;
    }
}
